from django.shortcuts import render

"""
1. 查询省份信息的sql语句 select * from tb_areas where parent_id is null;
    查询市的信息的sql语句 select * from tb_areas where parent_id = 440000;
    查询区(县)的信息的sql语句 select * from tb_areas where parent_id = 440300;
2. 回顾了序列化器中 关联序列化器
3. GenericAPIView 除了可以设置 queryset 和 serialzier_class 属性还可以重写方法
"""

#APIView
#GenericAPIView
#ListAPIView,RetirveAPIView

#视图集
from rest_framework.viewsets import ReadOnlyModelViewSet
from .models import Area
from .serializers import AreaSerializer,SubsAreaSerializer
from rest_framework_extensions.cache.mixins import ListCacheResponseMixin
from rest_framework_extensions.cache.mixins import CacheResponseMixin


class AreasInfoView(CacheResponseMixin,ReadOnlyModelViewSet):
    """
    GET     areas/infos/        获取省份信息
    GET     areas/infos/id/     获取市,区县信息
    """

    #查询结果集
    # queryset = Area.objects.all()
    #列表视图和详情视图  不能使用同一个查询集
    def get_queryset(self):
        #action属性就是 视图集的方法，list,retrieve,update,creat..
        if self.action == 'list':  #省份信息
            return Area.objects.filter(parent__isnull=True)
        else:   #retrieve
            #   市，区 县信息
            return Area.objects.all()

    #序列化器
    #serializer_class = AreaSerializer
    def get_serializer_class(self):
        if self.action == 'list':
            return AreaSerializer
        else:
            return SubsAreaSerializer
